package com.wcgksoft.controller.notice;

import com.wcgksoft.common.util.Layui;
import com.wcgksoft.entity.notic.Notic;
import com.wcgksoft.entity.system.EnterpriseProject;
import com.wcgksoft.entity.system.SysRole;
import com.wcgksoft.entity.system.SysRoleCode;
import com.wcgksoft.entity.system.SysUser;
import com.wcgksoft.service.SysRoleCodeService;
import com.wcgksoft.service.notic.NoticService;
import com.wcgksoft.service.system.SysRoleService;
import com.wcgksoft.service.system.UserService;
import net.sf.json.JSONObject;
import org.apache.shiro.SecurityUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping(value = "notice")
public class NoticeController {


    @Autowired
    private SysRoleCodeService sysRoleCodeService;

    @Autowired
    private SysRoleService sysRoleService;

    @Autowired
    private UserService sysUserService;

    @Autowired
    private NoticService noticService;


    /**
     * 查询角色
     * @return
     */
    @RequestMapping(value="list")
    public String myFindAll(ModelMap modelMap) {
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getSession().getAttribute("om_user");
        EnterpriseProject enterpriseProject = (EnterpriseProject) SecurityUtils.getSubject().getSession().getAttribute("area");
        ArrayList<SysUser> list = new ArrayList<SysUser>();
        if (sysUser.getSysRole().getSysRoleCode().getRole_number().equals("admin")){
            DetachedCriteria detachedCriteria = DetachedCriteria.forClass(SysRoleCode.class);
            detachedCriteria.add(Restrictions.eq("role_number", "enterprise"));
            List<SysRoleCode> byConditions = sysRoleCodeService.findByConditions(detachedCriteria);
            if (byConditions.size()>0){
                SysRoleCode sysRoleCode = byConditions.get(0);
                DetachedCriteria sysrole = DetachedCriteria.forClass(SysRole.class);
                sysrole.add(Restrictions.eq("sysRoleCode.id", sysRoleCode.getId()));
                List<SysRole> byConditions1 = sysRoleService.findByConditions(sysrole);
                if (byConditions1.size()>0){
                    for (SysRole sysRole : byConditions1) {
                        DetachedCriteria user = DetachedCriteria.forClass(SysUser.class);
                        user.add(Restrictions.eq("sysRole.id", sysRole.getId()));
                        List<SysUser> byConditions2 = sysUserService.findByConditions(user);
                        if (byConditions2.size()>0){
                            for (SysUser sysUser1 : byConditions2) {
                                list.add(sysUser1);
                            }
                        }
                    }
                }
            }
            modelMap.put("syslist", list);
        }

        if (sysUser.getSysRole().getSysRoleCode().getRole_number().equals("enterprise")){
            DetachedCriteria detachedCriteria = DetachedCriteria.forClass(SysRoleCode.class);
            detachedCriteria.add(Restrictions.eq("role_number", "manage"));
            List<SysRoleCode> byConditions = sysRoleCodeService.findByConditions(detachedCriteria);
            if (byConditions.size()>0){
                SysRoleCode sysRoleCode = byConditions.get(0);
                DetachedCriteria sysrole = DetachedCriteria.forClass(SysRole.class);
                sysrole.add(Restrictions.eq("sysRoleCode.id", sysRoleCode.getId()));
                List<SysRole> byConditions1 = sysRoleService.findByConditions(sysrole);
                if (byConditions1.size()>0){
                    for (SysRole sysRole : byConditions1) {
                        DetachedCriteria user = DetachedCriteria.forClass(SysUser.class);
                        user.add(Restrictions.eq("enterprise.id", sysUser.getEnterprise().getId()));
                        user.add(Restrictions.eq("sysRole.id", sysRole.getId()));
                        List<SysUser> byConditions2 = sysUserService.findByConditions(user);
                        if (byConditions2.size()>0){
                            for (SysUser sysUser1 : byConditions2) {
                                list.add(sysUser1);
                            }
                        }
                    }
                }
            }
            modelMap.put("syslist", list);
        }


        if (sysUser.getSysRole().getSysRoleCode().getRole_number().equals("manage")){
            DetachedCriteria detachedCriteria = DetachedCriteria.forClass(SysRoleCode.class);
            detachedCriteria.add(Restrictions.eq("role_number", "construction"));
            List<SysRoleCode> byConditions = sysRoleCodeService.findByConditions(detachedCriteria);
            if (byConditions.size()>0){
                SysRoleCode sysRoleCode = byConditions.get(0);
                DetachedCriteria sysrole = DetachedCriteria.forClass(SysRole.class);
                sysrole.add(Restrictions.eq("sysRoleCode.id", sysRoleCode.getId()));
                List<SysRole> byConditions1 = sysRoleService.findByConditions(sysrole);
                if (byConditions1.size()>0){
                    for (SysRole sysRole : byConditions1) {
                        DetachedCriteria user = DetachedCriteria.forClass(SysUser.class);
                        user.add(Restrictions.eq("sysRole.id", sysRole.getId()));
                        user.add(Restrictions.eq("enterprise.id", sysUser.getEnterprise().getId()));
                        user.add(Restrictions.eq("areaid", enterpriseProject.getId()));
                        List<SysUser> byConditions2 = sysUserService.findByConditions(user);
                        if (byConditions2.size()>0){
                            for (SysUser sysUser1 : byConditions2) {
                                list.add(sysUser1);
                            }
                        }
                    }
                }
            }
            modelMap.put("syslist", list);
        }


        if (list.size()>0){
            for (SysUser user : list) {
                System.out.println("公司名称"+user.getCompanyName());
            }
        }
        return "backlog/noticeadd";
    }



    /*
     * 发布通知
     *
     * */
    @ResponseBody
    @RequestMapping(value = "update")
    public Layui update(@RequestParam(value = "data", required = false) String data,@RequestParam(value = "checked", required = false) String checked ) {
        System.out.println("角色id=============================="+checked);
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getSession().getAttribute("om_user");
        JSONObject param = JSONObject.fromObject(data);
        if (param == null) {
            return Layui.data(1, "请求格式有误", 0, null);
        } else {
            Notic notic = (Notic) JSONObject.toBean(JSONObject.fromObject(data), Notic.class);
            notic.setTime(new Date());
            notic.setJunid(checked);
            String rname = "";
            if (checked.length()>0){
                if (checked.contains(",")){
                    String[] split = checked.split(",");
                    if (split.length>0){
                        for (String s : split) {
                            SysUser byId = sysUserService.findById(Integer.parseInt(s));
                            rname += byId.getName() + "、";
                        }
                    }
                }else{
                    SysUser byId = sysUserService.findById(Integer.parseInt(checked));
                    rname = byId.getName();
                }


            }
            notic.setSysuserid(sysUser.getId());
            SysUser byId = sysUserService.findById(sysUser.getId());
            notic.setName(byId.getName());
            notic.setReceivename(rname);
            noticService.saveOrUpdate(notic);
            return Layui.data(0, "修改成功", 1, Layui.addList(notic));
        }
    }




    /**
     * 通知列表
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "myFindAll")
    public Layui findAll(
            @RequestParam(value = "page", required = false) Integer page,
            @RequestParam(value = "limit", required = false) Integer limit) {
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getSession().getAttribute("om_user");
        ArrayList<Notic> notics = new ArrayList<>();
        DetachedCriteria criteria = DetachedCriteria.forClass(Notic.class);
        List<Notic> templateList = noticService.findByConditions(criteria, page, limit);
        if (templateList.size()>0){
            for (Notic notic : templateList) {
                String[] split = notic.getJunid().split(",");
                if (split.length>0){
                    for (String s : split) {
                       if (Integer.parseInt(s)==sysUser.getId()){
                           notics.add(notic);
                           continue;
                       }
                    }
                }
            }
        }
        return Layui.data(0, "查询成功", noticService.getTotalCount(), notics);

    }





    /**
     * 删除
     * @param id
     * @param
     * @return
     */
    @ResponseBody
    @RequestMapping(value="delete")
    public Layui delete(@RequestParam(value = "id") Integer id){
        Notic template=noticService.findById(id);
        noticService.delete(template);
        return Layui.data(1,"删除成功",1,null);
    }







    /**
     * 通知列表
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getAll")
    public Layui getall(
            @RequestParam(value = "page", required = false) Integer page,
            @RequestParam(value = "limit", required = false) Integer limit) {
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getSession().getAttribute("om_user");
        DetachedCriteria criteria = DetachedCriteria.forClass(Notic.class);
        criteria.add(Restrictions.eq("sysuserid", sysUser.getId()));
        List<Notic> byConditions = noticService.findByConditions(criteria);
        return Layui.data(0, "查询成功", noticService.getTotalCount(), byConditions);

    }



    /**
     * 删除
     * @param id
     * @param
     * @return
     */
    @ResponseBody
    @RequestMapping(value="deleteall")
    public Layui deleteall(@RequestParam(value = "id") Integer id){
        Notic template=noticService.findById(id);
        noticService.delete(template);
        return Layui.data(1,"删除成功",1,null);
    }
}
